import { Component } from '@angular/core';
import { IonicPage, ViewController } from 'ionic-angular';
import { SuperTabsController } from "ionic2-super-tabs";

import { ProductOptionListPage } from '../product-option-list/product-option-list';
import { GlobalData } from "../../providers/GlobalData";

/**
 * Generated class for the ProductOptionModalPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-product-option-modal',
  templateUrl: 'product-option-modal.html',
})
export class ProductOptionModalPage {

  productVariantList: Array<{
    id: number,
    dictionary: number,
    name: string,
    options: Array<{ id: number, name: string, price: string, sign: string, qty: number, type: string, selected: boolean }>
  }>;
  productOptionListPage: any = ProductOptionListPage;
  productOptionList: Array<{ id: number, name: string, price: string, sign: string, qty: number, type: string, selected: boolean }>;
  addonOptionList: Array<{ id: number, name: string, price: string, sign: string, qty: number, type: string, selected: boolean }>;
  discountType: string;

  constructor(public viewCtrl: ViewController,
    private superTabsCtrl: SuperTabsController,
    public global: GlobalData) {
    console.log("current product :", this.global.product);

    this.productVariantList = [];
    for (let i = 0; i < this.global.product['specifications'].length; i++) {
      if (this.global.product['specifications'][i]['enabled'] && this.global.product['specifications'][i]['selectible']) {
        let specOptinList: any;
        specOptinList = [];
        for (let spe of this.global.product['specifications'][i]['options']) {
          specOptinList.push({
            id: spe['id'],
            name: spe['name'],
            price: spe['price'],
            sign: spe['sign'],
            qty: 1,
            type: 'spec',
            selected: (this.global.product['options'] && this.global.product['options'][i]['options'][0] == spe['name']) ? true : false
          })
        }
        this.productVariantList.push({
          id: this.global.product['specifications'][i]['id'],
          dictionary: this.global.product['specifications'][i]['dictionary'],
          name: this.global.product['specifications'][i]['title'],
          options: specOptinList
        });
      }
    }
    if (this.global.product['addons'].length) {
      this.addonOptionList = [];
      for (let addon of this.global.product['addons']) {
        this.addonOptionList.push({
          id: addon['id'],
          name: addon['name'],
          price: addon['unit_price'],
          sign: '+',
          qty: addon['qty'],
          type: 'addons',
          selected: addon['qty'] > 0 ? true : false
        })
      }

      this.productVariantList.push({
        id: 0,
        dictionary: 0,
        name: 'Addons',
        options: this.addonOptionList
      });
    }
    this.discountType = '%';

  }

  // close modal
  closeModal() {
    let data = 'return data when close modal';
    this.viewCtrl.dismiss(data);
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad ProductOptionModalPage');
  }

}
